﻿package com.narunas.model.managers
{
    import org.puremvc.as3.interfaces.IProxy;
    import org.puremvc.as3.patterns.proxy.Proxy;
	import com.asual.swfaddress.SWFAddress;
	import com.asual.swfaddress.SWFAddressEvent;
	import flash.external.ExternalInterface;
	import org.puremvc.as3.patterns.observer.Notification;
	
    public class SWFAddressProxy extends Proxy implements IProxy
    {
        public static const NAME:String = 'SWFAddressProxy';
		
        public function SWFAddressProxy( )
        {
            super( NAME, Number(0) );
			SWFAddress.addEventListener(SWFAddressEvent.CHANGE, onAddressChange);
        }
		
		public function requestURI(uri:String):void
		{
			if (ExternalInterface.available) {
				SWFAddress.setValue(uri);
			} else {
				setTargetURI( SWFAddressUtil.segmentURI(uri) );
			}
		}
		
		public function setTitle(title:String):void
		{
			SWFAddress.setTitle(title);
		}
		
		private function onAddressChange(e:SWFAddressEvent):void
		{
			//trace("onAddressChange " + e.value);
			setTargetURI( SWFAddressUtil.segmentURI(e.value) );
		}
		
		private function setTargetURI(uriSegments:Array):void
		{
			//trace("setTargetURI " + uriSegments);
			sendNotification("$" + uriSegments[0], uriSegments, "Array");
		}
     }
}